home *** CD-ROM | disk | FTP | other *** search
/ All for Cell Phones: Sony Ericsson / Sony-Ericsson 2004.iso / Java / Chess / Fly_Chess.jar / ChessLogic.class (.txt) < prev    next >
Encoding:
Java Class File  |  2001-08-23  |  5.4 KB  |  241 lines

  1. import javax.microedition.lcdui.Font;
  2. import javax.microedition.lcdui.Graphics;
  3.  
  4. public class ChessLogic implements ServletResponseReceiver {
  5.    private boolean responsereceived = false;
  6.    private String response = "";
  7.    private ChessCanvas chesscanvas;
  8.    private ChessGUI chessgui;
  9.    private CommandsForm commandsform;
  10.    private ChessGame chessgame = null;
  11.    private Style12Parser currentboard = null;
  12.    private int click = 0;
  13.    private String movestring = "";
  14.    private int selectfield1 = 0;
  15.    private int selectfield2 = 0;
  16.  
  17.    public ChessLogic(ChessCanvas var1) {
  18.       this.chesscanvas = var1;
  19.       this.chessgui = new ChessGUI(var1);
  20.       this.commandsform = new CommandsForm(this, var1);
  21.       this.drawEmptyBoard();
  22.       this.registerCommands();
  23.    }
  24.  
  25.    public void alert(String var1) {
  26.       MessageForm var2 = new MessageForm("Alert", var1, this.commandsform);
  27.       var2.makeCurrent(CU.getDisplay());
  28.    }
  29.  
  30.    private void drawCurrentBoard() {
  31.       if (this.currentboard == null) {
  32.          this.drawEmptyBoard();
  33.       } else {
  34.          Graphics var4 = this.chessgui.getChessGraphics();
  35.  
  36.          for(int var5 = 8; var5 > 0; --var5) {
  37.             for(int var6 = 1; var6 < 9; ++var6) {
  38.                int var3 = var6 * 10 + var5;
  39.                int var1 = this.currentboard.getPiecePos(var3);
  40.                int var2 = this.currentboard.getColorPos(var3);
  41.                this.chessgui.drawPiece(var4, var1, var2, var3);
  42.             }
  43.          }
  44.  
  45.          if (this.selectfield1 != 0) {
  46.             this.chessgui.selectField(var4, this.selectfield1, 0, 0, 0, false);
  47.          }
  48.  
  49.          if (this.selectfield2 != 0) {
  50.             this.chessgui.selectField(var4, this.selectfield2, 255, 255, 255, false);
  51.          }
  52.  
  53.       }
  54.    }
  55.  
  56.    private void drawEmptyBoard() {
  57.       Graphics var2 = this.chessgui.getChessGraphics();
  58.  
  59.       for(int var3 = 8; var3 > 0; --var3) {
  60.          for(int var4 = 1; var4 < 9; ++var4) {
  61.             int var1 = var4 * 10 + var3;
  62.             this.chessgui.drawPiece(var2, 0, 0, var1);
  63.          }
  64.       }
  65.  
  66.    }
  67.  
  68.    private void drawInformation(Graphics var1) {
  69.       String var2 = "No info";
  70.       if (this.currentboard != null) {
  71.          if (this.isYourMove() && this.responsereceived && !this.movestring.equals("")) {
  72.             var2 = "Your move? " + this.movestring;
  73.          }
  74.  
  75.          if (this.isYourMove() && !this.responsereceived) {
  76.             var2 = "Sending move...";
  77.          }
  78.  
  79.          if (!this.isYourMove()) {
  80.             var2 = "Awaiting opponent move...";
  81.          }
  82.  
  83.          if (this.isYourMove() && this.responsereceived && this.movestring.equals("")) {
  84.             var2 = "Op. moved: " + this.currentboard.getPreviousMoveVerbose() + " your? ";
  85.          }
  86.       } else {
  87.          var2 = "No game active";
  88.       }
  89.  
  90.       this.chessgui.drawInformation(var1, 1, var2);
  91.    }
  92.  
  93.    private void error(String var1) {
  94.       MessageForm var2 = new MessageForm("Error", var1, this.chesscanvas);
  95.       var2.makeCurrent(CU.getDisplay());
  96.    }
  97.  
  98.    private void executeCommand(int var1) {
  99.       if (this.chessgame == null && var1 == 2) {
  100.          this.error("Please logon first");
  101.       } else {
  102.          switch (var1) {
  103.             case 1:
  104.                this.commandsform.makeCurrent(CU.getDisplay());
  105.                break;
  106.             case 2:
  107.                this.move();
  108.          }
  109.  
  110.       }
  111.    }
  112.  
  113.    public ChessGame getChessGame() {
  114.       return this.chessgame;
  115.    }
  116.  
  117.    public CommandsForm getCommandsForm() {
  118.       return this.commandsform;
  119.    }
  120.  
  121.    public void handleBoardClick(int var1, int var2) {
  122.       int var3 = this.chessgui.getFieldCode(var1, var2);
  123.       switch (this.click) {
  124.          case 0:
  125.             this.movestring = CU.int2alg(var3);
  126.             this.selectfield1 = var3;
  127.             this.selectfield2 = 0;
  128.             ++this.click;
  129.             break;
  130.          case 1:
  131.             this.movestring = this.movestring + "-" + CU.int2alg(var3);
  132.             ++this.click;
  133.             this.selectfield2 = var3;
  134.             break;
  135.          case 2:
  136.             this.movestring = CU.int2alg(var3);
  137.             this.click = 1;
  138.             this.selectfield1 = var3;
  139.             this.selectfield2 = 0;
  140.       }
  141.  
  142.    }
  143.  
  144.    public void handleClick(int var1, int var2) {
  145.       if (this.chessgui.inChessBoard(var1, var2)) {
  146.          this.handleBoardClick(var1, var2);
  147.       } else {
  148.          this.handleCommandClick(var1, var2);
  149.       }
  150.  
  151.       this.chesscanvas.repaint();
  152.       this.chesscanvas.serviceRepaints();
  153.    }
  154.  
  155.    public void handleCommandClick(int var1, int var2) {
  156.       if (this.chessgui.getCommand(var1, var2) != 0) {
  157.          this.executeCommand(this.chessgui.getCommand(var1, var2));
  158.       }
  159.  
  160.    }
  161.  
  162.    private boolean isYourMove() {
  163.       if (this.currentboard == null) {
  164.          return false;
  165.       } else if (this.currentboard.getBoardOrientation() == 1 && this.currentboard.getColorToMove() == 0) {
  166.          return true;
  167.       } else {
  168.          return this.currentboard.getBoardOrientation() == 0 && this.currentboard.getColorToMove() == 1;
  169.       }
  170.    }
  171.  
  172.    private void move() {
  173.       this.responsereceived = false;
  174.       if (this.chessgame.postActive()) {
  175.          this.alert("Unable to send command, still postthread active. \nStatus: " + this.chessgame.getStatus());
  176.       } else {
  177.          this.chessgame.doPost("move " + this.movestring, false);
  178.          this.movestring = "";
  179.          this.click = 0;
  180.       }
  181.  
  182.    }
  183.  
  184.    public void paint(Graphics var1) {
  185.       this.drawCurrentBoard();
  186.       var1.setFont((Font)null);
  187.       Font var2 = var1.getFont();
  188.       this.chessgui.drawCommands(var1);
  189.       this.chessgui.drawChessGraphics(var1);
  190.       Font var3 = Font.getFont(var2.getFace(), 0, 8);
  191.       var1.setFont(var3);
  192.       this.drawInformation(var1);
  193.    }
  194.  
  195.    public void receiveServletResponse(String var1) {
  196.       this.response = var1;
  197.       this.responsereceived = true;
  198.       Style12Parser var2 = new Style12Parser();
  199.       if (var2.parseStyle12(var1)) {
  200.          boolean var3 = true;
  201.          if (this.currentboard != null && this.currentboard.getPreviousMoveVerbose().equals(var2.getPreviousMoveVerbose())) {
  202.             var3 = false;
  203.          }
  204.  
  205.          this.currentboard = var2;
  206.          CU.setBoardOrientation(this.currentboard.getBoardOrientation());
  207.          if (var3) {
  208.             this.drawCurrentBoard();
  209.          }
  210.  
  211.          if (this.chessgame != null && !this.isYourMove() && this.chessgame.isPlaying()) {
  212.             this.responsereceived = false;
  213.             this.selectfield1 = 0;
  214.             this.selectfield2 = 0;
  215.             this.chessgame.doPost("nextstate", false);
  216.          }
  217.       }
  218.  
  219.       this.chesscanvas.repaint();
  220.       this.chesscanvas.serviceRepaints();
  221.    }
  222.  
  223.    private void registerCommands() {
  224.       this.chessgui.registerCommand(1, "MENU");
  225.       this.chessgui.registerCommand(2, "MOVE");
  226.    }
  227.  
  228.    public void reset() {
  229.       this.currentboard = null;
  230.       this.drawEmptyBoard();
  231.       this.movestring = "";
  232.       this.click = 0;
  233.       this.selectfield1 = 0;
  234.       this.selectfield2 = 0;
  235.    }
  236.  
  237.    public void setChessGame(ChessGame var1) {
  238.       this.chessgame = var1;
  239.    }
  240. }
  241.